#include "stdafx.h"
#include "UIAnimGroup.h"
#include "UIAnimPlayer.h"
#include "Lib2D/Sprite.h"
#include "Lib2D/SpriteManager.h"
#include "Lib2D/UIKit.h"
#include "Application.h"
#include "UIInfo.h"

UIAnimBG::UIAnimBG(const char *pSpriteName, int frameId, bool hasAnim):m_hasAnim(hasAnim)
,m_frameId(frameId)
,m_isFrame(true)
,m_name(pSpriteName)
,m_offsetY(0)
,m_color(0x0)
{
	if( hasAnim )
	{
		m_pPlayer = NEW UIAnimPlayer();
	}

	m_pTexture = NULL;
	m_pMaterial = NULL;

	if( frameId == -1 )
	{
		irr::video::IVideoDriver *driver = Application::instance()->GetDevice()->getVideoDriver();
		m_pTexture =  driver->getTexture(pSpriteName);
		if( m_pTexture )
		{
			m_pTexture->setMagFilter(irr::video::ETFT_NEAREST);
			m_pTexture->setMinFilter(irr::video::ETFT_NEAREST);
		}

		m_pMaterial = new irr::video::SMaterial();
		m_pMaterial->setFlag(irr::video::EMF_LIGHTING, false);
		m_pMaterial->setFlag(irr::video::EMF_2D_USE_TEXTURE_ALPHA, true);
		m_pMaterial->setMaterialType(irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
		m_pMaterial->setFlag(irr::video::EMF_ZBUFFER, false);
		m_pMaterial->setFlag(irr::video::EMF_ZWRITE_ENABLE, false);
	}
}

UIAnimBG::~UIAnimBG()
{
	UIAnimPlayer *pPlayer = GetAnimPlayer();
	if( pPlayer )
	{
		delete(pPlayer);
	}

	if( m_pTexture )
	{
		irr::video::IVideoDriver *pDriver = Application::instance()->GetDevice()->getVideoDriver();
		pDriver->removeTexture( m_pTexture );

		SAFE_DEL(m_pMaterial);
	}
}

void UIAnimBG::Update(float dt)
{
	UIAnimPlayer *pPlayer = GetAnimPlayer();
	if( pPlayer)
	{
		pPlayer->Update(dt);
	}
}

void UIAnimBG::Render()
{
	if( HasPlayingAnimation() )
	{
		DrawMyAnim();
		return;
	}

	if( m_frameId != -1 )
	{
		CSprite *pSprite = CSpriteManager::instance()->GetSprite( m_name.c_str() );
		if( pSprite )
		{
			pSprite->SetColor(m_color);
			if( m_isFrame )
			{
				pSprite->PaintFrame(m_frameId, 0,m_offsetY);
			}
		}
	}
	else
	{
		int w = UIInfo::instance()->GetScrW();
		int h = UIInfo::instance()->GetScrH();
		irr::core::dimension2d<irr::s32> dimOfTexture = m_pTexture->getSize();
		irr::video::IVideoDriver *driver = Application::instance()->GetDevice()->getVideoDriver();
		driver->draw2DImage(m_pTexture, irr::core::rect<irr::s32>( 0 ,0, w,h),
					irr::core::rect<irr::s32>(0,0,w, h) );
	}
}

void UIAnimBG::DrawMyAnim()
{
	if( m_pPlayer == NULL )
	{
		return;
	}

	UIAnimChannelData data;
	switch( m_state )
	{
	case E_TOUCH_STATE_NORMAL:
	case E_TOUCH_STATE_CREATE:
	case E_TOUCH_STATE_RELEASE:
	case E_TOUCH_STATE_TOUCH:
		if( m_pPlayer->GetChannelData(UI_ANIMATION::ChannelTypeImage, 0 , data ))
		{
			int x = 0 + data.m_values[UI_ANIMATION::TimelinePositionX];
			int y = 0 + data.m_values[UI_ANIMATION::TimelinePositionY];
			if( IsExistTargetInfo (E_TouchTarget_Image, 0 ) )
			{
				int targetX =0;
				int targetY =0;
				GetTargetPositionInfo(E_TouchTarget_Image, 0 ,targetX,targetY);
				x = targetX + data.m_values[UI_ANIMATION::TimelinePositionX];
				y = targetY + data.m_values[UI_ANIMATION::TimelinePositionY];
			}
			float rotZ = data.m_values[UI_ANIMATION::TimelineRotationZ];
			int alpha = 255 * data.m_values[UI_ANIMATION::TimelineAlpha];
			float scale = 1.0f * data.m_values[UI_ANIMATION::TimelineScale];
			if( m_frameId != -1 ) 
			{
				DrawTarget(E_TouchTarget_Image,0, x, y, rotZ, alpha,scale );
			}
			else
			{
				RenderTexture(m_pTexture,m_pMaterial, x,y,scale,rotZ);
			}
		}
		break;
	}
}

bool UIAnimBG::IsCreateProcessFinished()
{
	if( m_pPlayer )
	{
		return m_pPlayer->IsAnimEnd(UI_ANIMATION::AnimEnter);
	}
	return true;
}

bool UIAnimBG::IsNormalProcessFinished()
{
	if( m_pPlayer )
	{
		return m_pPlayer->IsAnimEnd(UI_ANIMATION::AnimNormal);
	}
	return true;
}

void UIAnimBG::ChangeBG(const char *pSpriteName, int frameId)
{
	m_frameId = frameId;
	m_name = pSpriteName;
}

UIAnimGroup::UIAnimGroup()
{
	m_allSub.clear();
}

UIAnimGroup::~UIAnimGroup()
{
	m_allSub.clear();
}


void UIAnimGroup::Add( TouchScreenBase * sub )
{
	m_allSub.push_back( sub );
}

void UIAnimGroup::RemoveAll()
{
	m_allSub.clear();
}

void UIAnimGroup::Update(float dt)
{
	UpdateTouchState();

	for(int i=0;i< m_allSub.size();i++)
	{
		TouchScreenBase *pBase = m_allSub[i];
		pBase->UpdateTouchState();
		UIAnimPlayer * pPlayer = pBase->GetAnimPlayer();
		if( pPlayer )
		{
			pPlayer->Update(dt);
		}
	}
}

bool UIAnimGroup::IsCreateProcessFinished()
{
	if( m_state != E_TOUCH_STATE_RELEASE )
	{
		return false;
	}

	if( m_allSub.size() == 0 )
	{
		return true;
	}

	bool bExit = true;
	for(int i=0;i< m_allSub.size();i++)
	{
		TouchScreenBase *pBase = m_allSub[i];
		if( !pBase->IsCreateProcessFinished() )
		{
			bExit = false;
		}
	}

	return bExit;
}

bool UIAnimGroup::IsExitProcessFinished()
{
	if( m_state != E_TOUCH_STATE_RELEASE )
	{
		return false;
	}

	if( m_allSub.size() == 0 )
	{
		return true;
	}

	bool bExit = true;
	for(int i=0;i< m_allSub.size();i++)
	{
		TouchScreenBase *pBase = m_allSub[i];
		if( !pBase->IsExitProcessFinished() )
		{
			bExit = false;
		}
	}

	return bExit;
}

void UIAnimGroup::ChangeTouchState( E_TouchState state, TouchScreenBase * sub)
{
	TouchScreenBase::ChangeTouchState(state);
	if( state == E_TOUCH_STATE_RELEASE )
	{
		for(int i=0;i< m_allSub.size();i++)
		{
			TouchScreenBase *pBase = m_allSub[i];
			if( sub == NULL || (sub && pBase == sub ) )
			{
				pBase->ChangeTouchState( state );
				UIAnimPlayer * pPlayer = pBase->GetAnimPlayer();
				if(pPlayer)
				{
					pPlayer->PlayAnim( UI_ANIMATION::AnimExit );
				}
			}
		}
	}
	else if( state == E_TOUCH_STATE_CREATE )
	{
		for(int i=0;i< m_allSub.size();i++)
		{
			TouchScreenBase *pBase = m_allSub[i];
			if( sub == NULL || (sub && pBase == sub ) )
			{
				pBase->ChangeTouchState( state );
				UIAnimPlayer * pPlayer = pBase->GetAnimPlayer();
				if(pPlayer)
				{
					pPlayer->PlayAnim( UI_ANIMATION::AnimEnter);
				}
			}
		}
	}
	else if( state == E_TOUCH_STATE_NORMAL )
	{
		for(int i=0;i< m_allSub.size();i++)
		{
			TouchScreenBase *pBase = m_allSub[i];
			pBase->ChangeTouchState( state );
		}
	}
}

void UIAnimGroup::Reset()
{
	if( m_pPlayer )
	{
		m_pPlayer->Reset();
	}
	for(int i=0;i< m_allSub.size();i++)
	{
		TouchScreenBase *pBase = m_allSub[i];
		pBase->Restore();

		UIAnimPlayer * pPlayer = pBase->GetAnimPlayer();
		if(pPlayer)
		{
			pPlayer->Reset();
		}		
	}
}

